/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.analysis.product.model;

import cn.ekuma.epos.bean.util.ProductLocationStateHelper;

import com.openbravo.bean.erp.viewbean.ProductAnalysis;
import com.openbravo.bean.erp.viewbean.ProductTransaction;
import com.openbravo.pos.base.AppLocal;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Administrator
 */
public class ProductAnalysisTableModel extends AbstractTableModel{
    
    List<ProductAnalysis> rows;
    ArrayList<String> transactionColumnName;

    public ProductAnalysisTableModel() {
        rows=new ArrayList();
        transactionColumnName=new ArrayList<String>();
    }

    public void resert( ArrayList<String> columns,List<ProductAnalysis> rows){
        transactionColumnName=columns;
        this.rows=rows;
        this.fireTableStructureChanged();
    }

    public int getRowCount() {
        return rows.size();
    }

    @Override
    public String getColumnName(int column) {
        if(column<COLUMNS_Pref.length)
            return COLUMNS_Pref[column];
        if(column<COLUMNS_Pref.length+transactionColumnName.size())
            return transactionColumnName.get(column-COLUMNS_Pref.length);
        return COLUMNS_After[column-COLUMNS_Pref.length-transactionColumnName.size()];
    }

    public int getColumnCount() {
        return COLUMNS_Pref.length+transactionColumnName.size()+COLUMNS_After.length;
    }

    public ProductAnalysis get(int index){
        return rows.get(index);
    }
    
    

    @Override
	public Class<?> getColumnClass(int column) {
    	if(column==1)
    		return Double.class;
        if(column>=COLUMNS_Pref.length&&column<COLUMNS_Pref.length+transactionColumnName.size())
            return Double.class;
		return super.getColumnClass(column);
	}



	public static String[] COLUMNS_Pref=new String[]{
           AppLocal.getIntString("label.prodname"),
           AppLocal.getIntString("label.storeNum"),
           AppLocal.getIntString("label.minimum"),
           AppLocal.getIntString("label.maximum"),
           AppLocal.getIntString("label.productLocationState")
           
    };
    
    public static String[] COLUMNS_After=new String[]{
    	AppLocal.getIntString("label.prodcategory"),
        AppLocal.getIntString("label.prodpricebuy"),
        AppLocal.getIntString("label.prodpricesell"),
        AppLocal.getIntString("label.baseCustomerPrice"),
        AppLocal.getIntString("label.WholesalePrice"),
        AppLocal.getIntString("label.prodref"),
        AppLocal.getIntString("label.prodbarcode"),
        AppLocal.getIntString("label.product.origin"),//产地
        AppLocal.getIntString("label.product.m_similarCode"),
        AppLocal.getIntString("label.prodcut.m_sManufacturer"),//品牌
        AppLocal.getIntString("label.product.m_iDurability"),//质保
        AppLocal.getIntString("label.produnits"),//计量单位
        AppLocal.getIntString("label.product.quantity"),//装数
        "demo"//补助的
 };

    public Object getValueAt(int rowIndex, int columnIndex) {
        ProductAnalysis selected=rows.get(rowIndex); 
        if(columnIndex<COLUMNS_Pref.length)
        	 switch (columnIndex) {
             case 0:
                 return selected.getM_sName();
             case 1:
                 return selected.getMultiply();
             case 2:
                 return selected.getMinimum();
             case 3:
                 return selected.getMaximum();
             case 4:
                 return ProductLocationStateHelper.getProductLocationState(selected.getState());
        	 }
        if(columnIndex<COLUMNS_Pref.length+transactionColumnName.size()){
        	int realColumnIndex=columnIndex-COLUMNS_Pref.length;
            ProductTransaction pTran=selected.getTransaxtion(transactionColumnName.get(realColumnIndex));
            return pTran==null?null:pTran.getMultiply();
        }
        switch (columnIndex-COLUMNS_Pref.length-transactionColumnName.size()) {
            case 0:
                 return selected.getCategory();
            case 1:
                return selected.getM_dPriceBuy();
            case 2:
                return selected.getM_dPriceSell();
            case 3:
                return selected.getM_dCustomerPrice();
            case 4:
                return selected.getM_dWholesalePrice();    
            case 5:
                return selected.getM_sRef();
            case 6:
                return selected.getM_sCode();
            case 7:
                return selected.getM_origin();
            case 8:
                return selected.getM_sSimilarCode();
            case 9:
                return selected.getM_sManufacturer();
            case 10:
                return selected.getM_iDurability();
            case 11:
                return selected.getM_unit();
            case 12:
                return selected.getM_quantity();
            case 13:
                return Math.round(selected.getMultiply());
        }
        return null;
    }
}
